package org.kust.bbs.search.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.kust.bbs.common.web.JsonPage;
import org.kust.bbs.search.entity.Item;
import org.kust.bbs.search.entity.User;
import org.kust.bbs.search.mapper.searchMapper;
import org.kust.bbs.search.repository.ItemRepository;
import org.kust.bbs.search.repository.UserRepository;
import org.kust.bbs.search.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ServiceLocalServiceImp implements ISearchService {

    //    @DubboReference // Dubbo消费模块
//    private IForFrontSpuService dubboSpuService;
    @Autowired
    private searchMapper invmapper;
    @Autowired
    private ItemRepository itemRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public void loadEsByPage() {

        int i = 1;
        int pages = 0;
        do {

            JsonPage<Item> spus = getItemByPage(i, 1);
            List<Item> esSpus = spus.getList();
            itemRepository.saveAll(esSpus);
            pages = spus.getTotalPage();
            log.info("成功新增第{}页Item数据", i);
            i++;
        } while (i <= pages);

        i = 1;
        pages = 0;

        do {

            JsonPage<User> Users = getUserByPage(i, 1);
            List<User> esUser = Users.getList();
            userRepository.saveAll(esUser);
            pages = Users.getTotalPage();
            log.info("成功新增第{}页User数据", i);
            i++;
        } while (i <= pages);

    }


    public JsonPage<Item> getItemByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Item> list = invmapper.findAllList();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    public JsonPage<User> getUserByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = invmapper.findUserList();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public JsonPage<Item> search(String keyword, Integer page, Integer pageSize) {
        // SpringData分页,参数0表示第一页,需要将page-1才能查询正确页码
        Page<Item> spus = itemRepository.queryItemsByTitlesMatchesOrCategoryMatches(
                keyword, keyword, PageRequest.of(page - 1, pageSize));
        // 业务逻辑层要求返回JsonPage类型,我们目前最简单的办法就是实例化JsonPage对象给它赋值
        JsonPage<Item> jsonPage = new JsonPage<>();
        // 当前页码
        jsonPage.setPage(page);
        jsonPage.setPageSize(pageSize);
        // 赋值总页数
        jsonPage.setTotalPage(spus.getTotalPages());
        // 赋值总条数
        jsonPage.setTotal(spus.getTotalElements());
        // 赋值分页数据
        jsonPage.setList(spus.getContent());
        // 别忘了返回JsonPage!!!
        return jsonPage;
    }

    public JsonPage<User> searchUser(String keyword, Integer page, Integer pageSize) {
        // SpringData分页,参数0表示第一页,需要将page-1才能查询正确页码
        Page<User> spus = userRepository.queryUsersByNicknameMatches(
                keyword, PageRequest.of(page - 1, pageSize));
        // 业务逻辑层要求返回JsonPage类型,我们目前最简单的办法就是实例化JsonPage对象给它赋值
        JsonPage<User> jsonPage = new JsonPage<>();
        // 当前页码
        jsonPage.setPage(page);
        jsonPage.setPageSize(pageSize);
        // 赋值总页数
        jsonPage.setTotalPage(spus.getTotalPages());
        // 赋值总条数
        jsonPage.setTotal(spus.getTotalElements());
        // 赋值分页数据
        jsonPage.setList(spus.getContent());
        // 别忘了返回JsonPage!!!
        return jsonPage;
    }
}